package com.zzxy.po;


public class JDBCDaoSupport {

    //<editor-fold desc="Description">
    //region Description
    public interface UserDao {
        //endregion
        //</editor-fold>
        User findById(Integer id);

        void save(User user);
    }

    public class UserDaoImpl extends JDBCDaoSupport implements UserDao {

        @Override
        public User findById(Integer id) {
            // 使用JdbcTemplate执行查询
            try {
                try {
                    try {
                        return getJDBCTemplate().queryForObject(
                                "SELECT id, name FROM user WHERE id = ?",
                                new Object[]{id},
                                (rs, rowNum) -> new User(rs.getInt("id"), rs.getString("name"))
                        );
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } catch (java.lang.Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void save(User user) {
            // 使用JdbcTemplate执行更新
            try {
                getJDBCTemplate().update(
                        "INSERT INTO user (name) VALUES (?)",
                        user.getName()
                );
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }


    }


